package tech.abing.spider.task.pageprocessor;

import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.regex.Pattern;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import tech.abing.spider.component.PageProcessor;
import tech.abing.spider.component.PageScript;
import tech.abing.spider.model.Proccessable;
import tech.abing.spider.page.OkPage;
import tech.abing.spider.page.Page;
import tech.abing.spider.request.BasicRequest;
import tech.abing.spider.request.PageRequest;
import tech.abing.spider.request.StartContext;
import tech.abing.spider.task.model.ProductID;
import tech.abing.spider.util.StringUtils;

public class ProductIDProccessor2 implements PageProcessor {

	private static Logger logger = Logger.getLogger(ProductIDProccessor2.class);
	
	static Pattern normalContain = Pattern.compile("天猫");//正常页面必定会存在的字符，你也可以使用页面上其他的字符
	
	static AtomicInteger atomicInteger = new AtomicInteger(0);
	
	@Override
	public PageScript getJavaScript() {
		return null;
	}

	@Override
	public Pattern getNormalContain() {
		return normalContain;
	}

	@Override
	public void process(OkPage page, StartContext context, List<BasicRequest> queue, List<Proccessable> objectContainer)
			throws Exception {
		
		Document doc = Jsoup.parse(page.getContent());
		
		String len = doc.select("b[class*=ui-page-s-len]").text();
		if("".equals(len)){
			len = doc.select("span[class*=page-info]").text();
		}
		
		logger.info("=========>当前页/总页数:"+ len);
		
		if(StringUtils.isNotBlank(len) && len.indexOf("/") > -1){
			
			int curPage = StringUtils.toInteger(len.substring(0, len.indexOf("/")));
			
			//获取获取当前页的产品
			Elements eles = doc.select("div[class~=item[2-6]line1]");
			
			String shopId = page.getRequest().getParamsByName("shopId").toString();
			String saleCount = page.getRequest().getParamsByName("saleCount").toString();
			String type = page.getRequest().getParamsByName("type").toString();
			boolean isContinue = true;
			
			for(int i = 0 ; i < eles.size() - 2; i++){
				
				Element ele = eles.get(i);
				
				Elements prodEles = ele.select("dl[class*=item]");
				
				for(Element prodEle : prodEles){
					
					int prodSaleCount = StringUtils.toInteger(prodEle.select("div[class*=sale-area] span[class*=sale-num]").text());
					if(prodSaleCount == 0){//销量为0的丢弃
						logger.info("=========>遇到产品销量为0,该店铺["+shopId+"]不在抓取");
						return;
					}
					
					ProductID prod = new ProductID();
					prod.setProdId(prodEle.attr("data-id").replaceAll("\\\\\"", ""));
					prod.setShopId(shopId);
					prod.setSaleCount(saleCount);
					prod.setProdSaleCount(prodSaleCount);
					prod.setType(type);
					objectContainer.add(prod);
					
					int count = atomicInteger.incrementAndGet();
					logger.info("采集成功产品ID数："+count);
				}
				
			}
			
			//判断是否有下一页
			Element nextEle = doc.select("a[class*=ui-page-s-next]").first();
			
			if(nextEle != null && isContinue){
				
				String preUrl = page.getRequest().getUrl();
				
				String nextUrl = preUrl + "&pageNo="+(curPage+1);
				
				if(preUrl.indexOf("pageNo=") > -1){
					nextUrl = preUrl.substring(0, preUrl.lastIndexOf("=")+1) +(curPage+1); 
				}
				
				 PageRequest req = context.createPageRequest(nextUrl, ProductIDProccessor2.class, 1);
				 req.putParams("shopId", shopId);
				 req.putParams("saleCount", saleCount);
				 req.putParams("type", type);
				 queue.add(req);//跟进队列
			}
			
		}
		
		
	}

	@Override
	public void processErrorPage(Page page, StartContext context,
			List<Proccessable> objectContainer) throws Exception {
		// TODO Auto-generated method stub
		
	}


}
